package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.common.R;
import com.itheima.domain.*;
import com.itheima.exception.BusinessException;
import com.itheima.service.*;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PmsProductManager {

    @DubboReference
    IProductService productService;

    @DubboReference
    IProductVertifyRecordService productVertifyRecordService;

    @DubboReference
    IMemberPriceService memberPriceService;

    @DubboReference
    IPrefrenceAreaProductRelationService prefrenceAreaProductRelationService;

    @DubboReference
    IProductAttributeValueService productAttributeValueService;

    @DubboReference
    IProductFullReductionService productFullReductionService;

    @DubboReference
    ISkuStockService skuStockService;

    @DubboReference
    ISubjectProductRelationService subjectProductRelationService;

    @DubboReference
    IProductLadderService productLadderService;

    public R findProductByPage(Integer pageNum, Integer pageSize, Integer brandId, String keyword, Integer productCategoryId, String productSn, Integer publishStatus) {
        PageBeanVo productByPage = productService.findProductByPage(pageNum, pageSize, brandId, keyword, productCategoryId, productSn, publishStatus);
        return R.success(productByPage);
    }

    public R createProduct(ProductParam productParam) {
        if (ObjectUtil.isEmpty(productParam)) {
            throw new BusinessException("系统异常");
        }

        Product product=new Product();
        BeanUtil.copyProperties(productParam,product);
        productService.createProduct(product);

        List<MemberPrice> memberPriceList = productParam.getMemberPriceList();
        if (ObjectUtil.isNotEmpty(memberPriceList)) {
            //循环添加
            for (MemberPrice pmsMemberPrice : memberPriceList) {
                memberPriceService.createMemberPrice(pmsMemberPrice);
            }
        }
//调用 CmsPrefrenceAreaProductRelationApi 添加 CmsPrefrenceAreaProductRelation 所对应表里的数据
        List<PrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productParam.getPrefrenceAreaProductRelationList();
        if (ObjectUtil.isNotEmpty(prefrenceAreaProductRelationList)) {
            //循环添加
            for (PrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                prefrenceAreaProductRelationService.createCmsPrefrenceAreaProductRelation(cmsPrefrenceAreaProductRelation);
            }
        }
        //调用 PmsProductAttributeValueApi 添加 PmsProductAttributeValue 所对应表里的数据
        List<ProductAttributeValue> productAttributeValueList = productParam.getProductAttributeValueList();
        if (ObjectUtil.isNotEmpty(productAttributeValueList)) {
            //循环添加
            for (ProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                productAttributeValueService.createPmsProductAttributeValue(pmsProductAttributeValue);
            }
        }
        //调用 PmsProductFullReductionApi 添加 PmsProductFullReduction 所对应表里的数据
        List<ProductFullReduction> productFullReductionList = productParam.getProductFullReductionList();
        if (ObjectUtil.isNotEmpty(productFullReductionList)) {
            //循环添加
            for (ProductFullReduction pmsProductFullReduction : productFullReductionList) {
                productFullReductionService.createPmsProductFullReduction(pmsProductFullReduction);
            }
        }
        //调用 PmsProductLadderApi 添加 PmsProductLadder 所对应表里的数据
        List<ProductLadder> productLadderList = productParam.getProductLadderList();
        if (ObjectUtil.isNotEmpty(productLadderList)) {
            //循环添加
            for (ProductLadder pmsProductLadder : productLadderList) {
                productLadderService.createPmsProductLadder(pmsProductLadder);
            }
        }
        //调用 PmsSkuStockApi 添加 PmsSkuStock 所对应表里的数据
        List<SkuStock> skuStockList = productParam.getSkuStockList();
        if (ObjectUtil.isNotEmpty(skuStockList)) {
            for (SkuStock pmsSkuStock : skuStockList) {
                skuStockService.createPmsSkuStock(pmsSkuStock);
            }
        }
        //调用 CmsSubjectProductRelationApi 添加 CmsSubjectProductRelation 所对应表里的数据
        List<SubjectProductRelation> subjectProductRelationList = productParam.getSubjectProductRelationList();
        if (ObjectUtil.isNotEmpty(subjectProductRelationList)) {
            for (SubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                subjectProductRelationService.createCmsSubjectProductRelation(cmsSubjectProductRelation);
            }
        }

        return R.success(null);
    }

    public R findByNameOrId(String keyword) {
        Product byNameOrId = productService.findByNameOrId(keyword);
        return R.success(byNameOrId);

    }

    public R updateDeleteStatus(Integer[] ids, Integer deleteStatus) {
        productService.updateDeleteStatus(ids,deleteStatus);
        return R.success(null);
    }

    public R setNew(Integer[] ids, Integer newStatus) {
        productService.setNew(ids,newStatus);
        return R.success(null);
    }

    public R upAndDown(Integer[] ids, Integer publishStatus) {
        productService.upAndDown(ids,publishStatus);
        return R.success(null);
    }

    public R recommend(Integer[] ids, Integer recommendStatus) {
        productService.recommend(ids,recommendStatus);
        return R.success(null);
    }

    public R verify(Integer[] ids, Integer verifyStatus, String detail) {
        productVertifyRecordService.verify(ids,verifyStatus,detail);
        return R.success(null);
    }

    public R updateProduct(Integer id, Product productParam) {
        productService.updateProduct(id,productParam);
        return R.success(null);
    }

    public R getUpdateMessage(Integer id) {

        Product product = productService.selectById(Long.valueOf(id));

        ProductResult productResult=new ProductResult();
        BeanUtil.copyProperties(product, productResult);
        List<MemberPrice> memberPriceList= memberPriceService.selectByProductId(Long.valueOf(id));

        productResult.setMemberPriceList(memberPriceList);
        List<PrefrenceAreaProductRelation> prefrenceAreaProductRelations = prefrenceAreaProductRelationService.selectByProductId(Long.valueOf(id));

        productResult.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelations);

        List<ProductAttributeValue> productAttributeValues = productAttributeValueService.selectByProductId(Long.valueOf(id));
        productResult.setProductAttributeValueList(productAttributeValues);

        List<ProductFullReduction> productFullReductions = productFullReductionService.selectByProductId(Long.valueOf(id));
        productResult.setProductFullReductionList(productFullReductions);

        List<SkuStock> skuStocks = skuStockService.selectByProductId(Long.valueOf(id));
        productResult.setSkuStockList(skuStocks);

        List<SubjectProductRelation> subjectProductRelations = subjectProductRelationService.selectByProductId(Long.valueOf(id));
        productResult.setSubjectProductRelationList(subjectProductRelations);
        return R.success(productResult);

    }


}
